package com.king.headline.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.king.headline.R;
import com.king.headline.UI.UserActivity;
import com.king.headline.beans.MainInfo;
import com.king.headline.beans.UserInfo;
import com.king.headline.net.Callback;
import com.king.headline.net.NetWork;
import com.king.headline.util.AccountManager;
import com.king.headline.util.Configuration;
import com.king.headline.util.CustomToast;
import com.king.headline.util.DialogUtils;
import com.king.headline.util.MyTextUtils;
import com.king.headline.util.TextViewUtils;
import com.king.headline.util.UIUtils;
import com.king.headline.util.Utils;
import com.king.headline.view.CircleImageView;
import com.king.headline.view.viewPager.OnMyListener;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by king on 2016/6/1.
 */
public class MainAdapter extends BaseAdapter {

    private TextViewUtils textViewUtils;
    private ImageLoader imageLoader;
    private ArrayList<MainInfo> _listMsg;
    private Context _context;
    private boolean isShowCollection;
    private OnMyListener myListener;
    private boolean isClickUserPage = true;

    public MainAdapter(Context context, ArrayList<MainInfo> listMsg) {
        _context = context;
        _listMsg = listMsg;
        imageLoader = UIUtils.getImageLoader();
        textViewUtils = TextViewUtils.getInstance();
    }
    public void setDate(ArrayList<MainInfo> listMsg) {
        _listMsg = listMsg;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return _listMsg.size();
    }

    @Override
    public Object getItem(int position) {
        return _listMsg.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        MainInfo mainDetailInfo = _listMsg.get(position);
        return mainDetailInfo.type;
    }

    @Override
    public int getViewTypeCount() {
        return 3;//复杂listview 有两种类型
    }

    /**
     * 是否展示收藏按钮
     * @param isShowCollection
     */
    public void isShowCollection(boolean isShowCollection){
        this.isShowCollection = isShowCollection;
    }
    public void isClickUserPage(boolean isClickUserPage){
        this.isClickUserPage = isClickUserPage;
    }

    public void setCollectChangeListener(OnMyListener myListener){
        this.myListener = myListener;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Cache cache;
        final MainInfo mainDetailInfo = _listMsg.get(position);
        cache = new Cache();
        switch (mainDetailInfo.type) {
            case 1:
                if (convertView == null) {

                    convertView = LayoutInflater.from(_context).inflate(R.layout.item_home1, null);
                    cache.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
                    cache.tv_sourceName = (TextView) convertView.findViewById(R.id.tv_sourceName);
                    cache.iv_img = (ImageView) convertView.findViewById(R.id.iv_img);
                    cache.tv_watchNumber = (TextView) convertView.findViewById(R.id.tv_watchNumber);
                    cache.iv_collection = (ImageView) convertView.findViewById(R.id.iv_collection);
                    cache.ll_bg = (LinearLayout) convertView.findViewById(R.id.ll_bg);
                    convertView.setTag(cache);
                } else {
                    cache = (Cache) convertView.getTag();
                }
                textViewUtils.setBlod(cache.tv_title);
                cache.tv_title.setText(mainDetailInfo.title);
                cache.tv_sourceName.setText("来自：" + mainDetailInfo.source.username);
                if (!mainDetailInfo.img.equals(cache.iv_img.getTag())) {
                    // 如果不相同，就加载。现在在这里来改变闪烁的情况
                    imageLoader.displayImage(mainDetailInfo.img, cache.iv_img, UIUtils.getSimpleOption());
                    cache.iv_img.setTag(mainDetailInfo.img);
                }

                cache.ll_bg.setTag(mainDetailInfo);
                cache.ll_bg.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        MainInfo mainDetailInfo = (MainInfo) view.getTag();
                        goDetailActivity(mainDetailInfo.id);

                    }
                });
                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) cache.ll_bg.getLayoutParams();
                layoutParams.height = (int) (Utils.getScreenWidth() * 0.3);
                cache.ll_bg.setLayoutParams(layoutParams);
                break;
            case 2:
                if (convertView == null) {
                    convertView = LayoutInflater.from(_context).inflate(R.layout.item_home2, null);
                    cache.tv_sourceName = (TextView) convertView.findViewById(R.id.tv_sourceName);
                    cache.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
                    cache.iv_img = (ImageView) convertView.findViewById(R.id.iv_img);
                    cache.iv_source = (CircleImageView) convertView.findViewById(R.id.iv_source);
                    cache.view_item = (View) convertView.findViewById(R.id.view_item);
                    cache.iv_collection = (ImageView) convertView.findViewById(R.id.iv_collection);
                    cache.tv_watchNumber = (TextView) convertView.findViewById(R.id.tv_watchNumber);
                    cache.tv_videoTime = (TextView) convertView.findViewById(R.id.tv_videoTime);
                    cache.tv_commentNumber = (TextView) convertView.findViewById(R.id.tv_commentNumber);

                    convertView.setTag(cache);
                } else {
                    cache = (Cache) convertView.getTag();
                }
                textViewUtils.setBlod(cache.tv_title);
                cache.tv_title.setText(mainDetailInfo.title);
                cache.tv_sourceName.setText("" + mainDetailInfo.source.username);
                new MyTextUtils().setText(cache.tv_videoTime,mainDetailInfo.videoTime);
                mainDetailInfo.source.chatHeadurl = mainDetailInfo.source.chatHeadurl;
                if (!mainDetailInfo.img.equals(cache.iv_img.getTag())) {
                    // 如果不相同，就加载。现在在这里来改变闪烁的情况
                    imageLoader.displayImage( mainDetailInfo.img, cache.iv_img, UIUtils.getSimpleOption());
                    cache.iv_img.setTag(mainDetailInfo.img);
                }
                if (!mainDetailInfo.source.chatHeadurl.equals(cache.iv_source.getTag())) {
                    // 如果不相同，就加载。现在在这里来改变闪烁的情况
                    imageLoader.displayImage(Configuration.API_HOST+mainDetailInfo.source.chatHeadurl, cache.iv_source, UIUtils.getSimpleOption());
                    cache.iv_source.setTag(mainDetailInfo.source.chatHeadurl);
                }
                cache.view_item.setTag(mainDetailInfo);
                cache.view_item.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        MainInfo mainDetailInfo = (MainInfo) view.getTag();
                        goDetailActivity(mainDetailInfo.id);
                    }
                });

                RelativeLayout.LayoutParams layoutParams1 = (RelativeLayout.LayoutParams) cache.view_item.getLayoutParams();
                layoutParams1.height = (int) (Utils.getScreenWidth() * 0.7);
                cache.view_item.setLayoutParams(layoutParams1);


                break;
        }
        cache.tv_watchNumber.setText(mainDetailInfo.watchNumber + "");
        if (isShowCollection){
            cache.iv_collection.setVisibility(View.VISIBLE);
            cache.iv_collection.setTag(mainDetailInfo);
            cache.iv_collection.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(final View view) {
                    final MainInfo mainDetailInfo = (MainInfo) view.getTag();
                    DialogUtils.showHintDialogTwo(_context, "确定取消收藏吗？", new DialogUtils.OnDialogListener() {
                        @Override
                        public void onOk(View v) {
                            videosCollections(mainDetailInfo);
                        }

                        @Override
                        public void onCancel(View v) {

                        }
                    });

                }

            });

        }else{
            cache.iv_collection.setVisibility(View.GONE);
        }

        //个人页的点击事件
        if (isClickUserPage) {
            cache.tv_sourceName.setTag(mainDetailInfo);
            cache.tv_sourceName.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    MainInfo mainDetailInfo = (MainInfo) view.getTag();
                    goUserPageActivity(mainDetailInfo.source.id);
                }
            });
            if (cache.iv_source!=null) {
                cache.iv_source.setTag(mainDetailInfo);
                cache.iv_source.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        MainInfo mainDetailInfo = (MainInfo) view.getTag();
                        goUserPageActivity(mainDetailInfo.source.id);
                    }
                });
            }
        }

        cache.tv_commentNumber.setText(mainDetailInfo.commentsCount+"");

        return convertView;


    }

    private class Cache {
        public TextView tv_title;
        public TextView tv_sourceName;
        public ImageView iv_img;
        public TextView tv_watchNumber;
        public ImageView iv_collection;
        public LinearLayout ll_bg;
        public CircleImageView iv_source;
        public View view_item;
        public TextView tv_videoTime;
        public TextView tv_commentNumber;
    }

    private void videosCollections(final MainInfo mainDetailInfo) {
        UserInfo userInfo = AccountManager.getInstance().getUser();
        NetWork.getInstance().videosCollections(userInfo.encyptdId, userInfo.apikey,
                mainDetailInfo.id, 2, new Callback() {
                    @Override
                    public void success(String result) {
                        CustomToast.showToast(result);
//                        MainAdapter.this.notifyDataSetChanged();
                        myListener.method();
                    }


                    @Override
                    public void notice(JSONObject jsonObject, String message) {
                        CustomToast.showToast(message);
                    }

                    @Override
                    public void failed(String e) {
                        CustomToast.showToast(e);
                    }
                });
    }
    private  void goDetailActivity(String id){
//        Intent intent = new Intent(_context, DetailActivity.class);
//        intent.putExtra("sourceid",id);
//        _context.startActivity(intent);;
    }
    private  void goUserPageActivity(String id){
        Intent intent = new Intent(_context, UserActivity.class);
        intent.putExtra("sourceid",id);
        _context.startActivity(intent);;
    }

}

